//
//  HJPhotoOrVideoRightMsgCell.m
//  project
//
//  Created by hu on 2019/3/27.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJPhotoOrVideoRightMsgCell.h"

@interface HJPhotoOrVideoRightMsgCell ()
/*
 * 头像父视图
 */
@property (weak, nonatomic) IBOutlet UIView *headSubView;
/*
 * 头像图片
 */
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
/*
 * 内容图片
 */
@property (weak, nonatomic) IBOutlet UIImageView *contentImage;
/*
 * 播放按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *playBtn;

@property (weak, nonatomic) IBOutlet UIButton *sendError;
@property (nonatomic,strong) RCMessage * rcMsg;
@end

@implementation HJPhotoOrVideoRightMsgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    // 用户图片 圆角
    [self.headImage rounded:(UIRectCornerAllCorners) radius:20];
    // 图片外的圆环
    [self.headSubView rounded:(UIRectCornerAllCorners) radius:20 borderColor:[UIColor lightGrayColor] borderWidth:1];
    [self.contentImage rounded:(UIRectCornerTopLeft | UIRectCornerBottomLeft |UIRectCornerBottomRight) radius:18.5];
    self.sendError.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.contentImage.contentMode = UIViewContentModeScaleAspectFill;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
    @weakify(self)
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        @strongify(self)
        if (self.didRightSelectImage) {
            self.didRightSelectImage(self.contentImage);
        }
    }];
    self.contentImage.userInteractionEnabled = YES;
    [self.contentImage addGestureRecognizer:tap];
}

/*
 * 播放按钮点击事件
 */
- (IBAction)playBtnClicked:(UIButton *)sender {
    
}
// 发送失败
- (IBAction)sendErrorBtnClicked:(UIButton *)sender {
    HJUserModel * userModel = [HJUserManager getUserModel];
    if ([self.rcMsg.content isMemberOfClass:[RCImageMessage class]]) {// 图片
        RCImageMessage *imageMsg = (RCImageMessage *)self.rcMsg.content;
        imageMsg.senderUserInfo = [[RCUserInfo alloc]initWithUserId:userModel.userId name:userModel.nickName portrait:userModel.avatar];
        [self uploadImageWithMsg:imageMsg];
    }else if ([self.rcMsg.content isMemberOfClass:[RCSightMessage class]]) { // 小视频
        RCSightMessage * sightMsg = (RCSightMessage *)self.rcMsg.content;
        sightMsg.senderUserInfo = [[RCUserInfo alloc]initWithUserId:userModel.userId name:userModel.nickName portrait:userModel.avatar];
        [self uploadVideoWithMsg:sightMsg];
    }
}
-(void)uploadVideoWithMsg:(RCSightMessage *)msg {
    RCSightMessage * sightMsg = (RCSightMessage *)self.rcMsg.content;
//    RCSightMessage * sightMsg = [RCSightMessage messageWithLocalPath:videoURL.path thumbnail:[UIImage imageNamed:@""] duration:15];
    [[RCIMClient sharedRCIMClient] sendMediaMessage:ConversationType_PRIVATE targetId:self.targetId content:sightMsg pushContent:nil pushData:nil uploadPrepare:^(RCUploadMediaStatusListener *uploadListener) {
    } progress:^(int progress, long messageId) {
        NSLog(@"进度==%d",progress);
    } success:^(long messageId) {
        NSLog(@"发送成功。当前消息ID：%ld", messageId);
    } error:^(RCErrorCode errorCode, long messageId) {
        NSLog(@"发送失败。消息ID：%ld， 错误码：%ld", messageId, (long)errorCode);
    } cancel:nil];
}
-(void)uploadImageWithMsg:(RCImageMessage *)msg {
    WS(weakSelf)
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * okAlert = [UIAlertAction actionWithTitle:@"重新发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[RCIMClient sharedRCIMClient] sendMediaMessage:ConversationType_PRIVATE targetId:self.targetId content:msg pushContent:@"图片" pushData:nil progress:^(int progress, long messageId) {
            NSLog(@"进度==%d",progress);
        } success:^(long messageId) {
            NSLog(@"发送成功。当前消息ID：%ld", messageId);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.sendError.hidden = YES;
            });
        } error:^(RCErrorCode errorCode, long messageId) {
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.sendError.hidden = NO;
            });
            NSLog(@"发送失败。消息ID：%ld， 错误码：%ld", messageId, (long)errorCode);
        } cancel:nil];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAlert];
    [alert addAction:cancel];
    [[UIViewController currentViewController] presentViewController:alert animated:YES completion:nil];
    
}
-(void)loadRightImageOrVideoWitMsg:(RCMessage *)msg {
    self.rcMsg = msg;
    HJUserModel * userModel = [HJUserManager getUserModel];
    NSLog(@"%@",msg.objectName);
    if ([msg.content isMemberOfClass:[RCImageMessage class]]) {
        self.playBtn.hidden = YES;
        RCImageMessage *imageMsg = (RCImageMessage *)msg.content;
        self.contentImage.image = [UIImage imageWithData:imageMsg.originalImageData];
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
    }else if ([msg.content isMemberOfClass:[RCSightMessage class]]) {
        self.playBtn.hidden = NO;
        RCSightMessage *sightMsg = (RCSightMessage *)msg.content;
        self.contentImage.image = sightMsg.thumbnailImage;
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
    }
    if (msg.sentStatus == SentStatus_FAILED) {
        self.sendError.hidden = NO;
    }else {
        self.sendError.hidden = YES;
    }
    
}
@end
